<!DOCTYPE HTML>
<html><head>
<title>Date Series test</title>
<script src="date-util.js"></script>
<script src="date-series.js"></script>
<style>
.pass { background: #efe }
.fail { background: #fee }
td { white-space: pre-wrap }
</style>
</head>

<body>
<h1>Date Series</h1>

<table id="tests" cellpadding=4 cellspacing=0 border=1>
  <tr>
    <th>Spec</th>
    <th>Start</th>
    <th>Str</th>
    <th>Dates</th>
  </tr>
  <tr>
    <td>{}</td>
    <td>2012-02-17</td>
    <td>Every year on the 17th of Feb.</td>
    <td>2012-2-17, 2013-2-17, 2014-2-17, 2015-2-17, 2016-2-17</td>
  </tr>
  <tr>
    <td>{ freq: FREQ_MONTHLY }</td>
    <td>2012-09-17</td>
    <td>Every month on the 17th</td>
    <td>2012-9-17, 2012-10-17, 2012-11-17, 2012-12-17, 2013-1-17</td>
  </tr>
  <tr>
    <td>{ freq: FREQ_MONTHLY, daysOfMonth: [15,-16] }</td>
    <td>2012-09-12</td>
    <td>Every month on the 15th, and the 16th to last day</td>
    <td>2012-9-15, 2012-10-15, 2012-10-16, 2012-11-15, 2012-12-15, 2012-12-16, 2013-1-15</td>
  </tr>
  <tr>
    <td>{ freq: FREQ_MONTHLY, interval: 2, daysOfMonth: [1, 2, 3, 4, -1] }</td>
    <td>2012-09-12</td>
    <td>Every 2 months on the 1st, the 2nd, the 3rd, the 4th, and the last day</td>
    <td>2012-9-30, 2012-11-1, 2012-11-2, 2012-11-3, 2012-11-4, 2012-11-30</td>
  </tr>
  <tr>
    <td>{ freq: FREQ_MONTHLY, daysOfMonth: [1, 2, 3, 4, -1], daysOfWeek: [1, 2, 3, 4, 5] }</td>
    <td>2012-11-1</td>
    <td>Every month on weekdays that are on the 1st, the 2nd, the 3rd, the 4th, and the last day</td>
    <td>2012-11-1, 2012-11-2, 2012-11-30, 2012-12-3, 2012-12-4, 2012-12-31, 2013-1-1, 2013-1-2, 2013-1-3, 2013-1-4, 2013-1-31</td>
  </tr>
  <tr>
    <td>{ freq: FREQ_WEEKLY, daysOfWeek: [1, 3, 5] }</td>
    <td>2012-7-1</td>
    <td>Every week on Mondays, Wednesdays, and Fridays</td>
    <td>2012-7-2, 2012-7-4, 2012-7-6, 2012-7-9, 2012-7-11, 2012-7-13</td>
  </tr>
  <tr>
    <td>{ freq: FREQ_WEEKLY, interval: 3, daysOfWeek: [2, 4] }</td>
    <td>2012-7-1</td>
    <td>Every 3 weeks on Tuesdays, and Thursdays</td>
    <td>2012-7-3, 2012-7-5, 2012-7-24, 2012-7-26, 2012-8-14, 2012-8-16, 2012-9-4</td>
  </tr>
  <tr>
    <td>{ freq: FREQ_WEEKLY }</td>
    <td>2012-7-2</td>
    <td>Every week on Mondays</td>
    <td>2012-7-2, 2012-7-9, 2012-7-16, 2012-7-23, 2012-7-30, 2012-8-6</td>
  </tr>
  <tr>
    <td>{ freq: FREQ_WEEKLY, interval: 2 }</td>
    <td>2012-7-2</td>
    <td>Every 2 weeks on Mondays</td>
    <td>2012-7-2, 2012-7-16, 2012-7-30, 2012-8-13, 2012-8-27</td>
  </tr>
  <tr>
    <td>{ freq: FREQ_DAILY, interval: 3, daysOfMonth: [13] }</td>
    <td>2012-7-2</td>
    <td>Every 3 days on the 13th of the month</td>
    <td>2012-8-13, 2013-1-13, 2013-4-13, 2013-5-13, 2013-9-13</td>
  </tr>
  <tr>
    <td>{ freq: FREQ_MONTHLY, daysOfMonth: [29], interval: 6 }</td>
    <td>2012-8-01</td>
    <td>Every 6 months on the 29th</td>
    <td>2012-8-29, 2013-8-29, 2014-8-29, 2015-8-29, 2016-2-29, 2016-8-29</td>
  </tr>
  <tr>
    <td>{"freq":0,"interval":27,"wkst":0}</td>
    <td>2012-1-27</td>
    <td>Every 27 days</td>
    <td>2012-1-27, 2012-2-23, 2012-3-21, 2012-4-17, 2012-5-14, 2012-6-10, 2012-7-7, 2012-8-3, 2012-8-30, 2012-9-26</td>
  </tr>
</table>

<script>(function () {
  function $(id) { return document.getElementById(id); }

  var table = $('tests');
  var rows = table.getElementsByTagName("tr");
  for (var i = 0, n = rows.length; i < n; ++i) {
    var cells = rows[i].getElementsByTagName("td");
    if (cells.length === 0) { continue; }
    var spec = cells[0].textContent;
    var start = cells[1].textContent.split("-");
    start = new Date(Date.UTC(+start[0], start[1] - 1, +start[2]));
    var str = cells[2].textContent;
    var dates = cells[3].textContent.split(/, /g);

    var series = dateSeries(start, eval("(" + spec + ")"));

    var seriesStr = series.toString();
    var seriesDates = [];
    (function () {
      var nToEnum = dates.length;
      var generator = series.generator();
      function fmt(d) {
        return d.getUTCFullYear() + "-" + (d.getUTCMonth() + 1)
            + "-" + d.getUTCDate();
      }
      for (var i = 0; i < nToEnum; ++i) {
        seriesDates.push(fmt(generator()));
      }
    })();

    function checkResult(cell, golden, actual) {
      cell.className = (golden === actual) ? 'pass' : 'fail';
      if (golden !== actual) {
        cell.appendChild(cell.ownerDocument.createElement("hr"));
        cell.appendChild(cell.ownerDocument.createTextNode(actual));
      }
    }

    checkResult(cells[2], str, seriesStr);
    checkResult(cells[3], dates.join(", "), seriesDates.join(", "));
 }
})();</script>

</body></html>
